package zwp.exercise.intereview.base.binary_search;

import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author zwp14
 * @create 2024/6/27 19:54
 * @description 二分查找练习题
 */
public class BinarySearchExercise {
    public static void main(String[] args) {
        int i = method1();
        if (i == -1){
            System.out.println("所查询数字未在当前数组中!");
        }
    }

    private static int method1() {
        int[] array = {1, 5, 8, 11, 19, 22, 31, 35, 40, 45, 48, 49, 50};
        int target = 48;

        int idxStart = 0;
        int idxEnd = array.length - 1;
        int count = 0;

        while (idxStart <= idxEnd) {
            count++;
            int idxHalf = (idxStart + idxEnd) >>> 1;

            if (array[idxHalf] == target) {
                System.out.println("找到数字:"+array[idxHalf]+"索引:"+idxHalf+"查找次数:"+count);
                return 1;
            }else if (array[idxHalf] < target){
                idxStart = idxHalf + 1;
            }else if (array[idxHalf] > target){
                idxEnd = idxHalf - 1;
            }
        }
        return -1;
    }
}
